<template>
  <div class="mod-study__event">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item label="模糊搜索">
        <el-input v-model="state.dataForm.keywords" placeholder="请输入" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="state.getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('study:event:save')" type="primary"
          @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-tabs v-model="state.dataForm.status" class="demo-tabs" @tab-change="handleTabChange">
      <el-tab-pane :label="item.label" :name="item.value" v-for="(item, index) in studyStatusList" :key="index" />
    </el-tabs>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" row-key="id" border style="width: 100%">
      <el-table-column prop="name" label="研学名称" header-align="center"></el-table-column>
      <el-table-column prop="studyType" label="研学类型" header-align="center">
        <template v-slot="scope">
          <span>{{ getStudyTypeName(scope.row.studyType) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="projectName" label="研学所属项目" header-align="center"></el-table-column>
      <el-table-column prop="carryStartDate" label="研学开展时间" header-align="center">
        <template v-slot="scope">
          <span>{{ scope.row.carryStartDate }}~{{ scope.row.carryEndDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" header-align="center" align="center">
        <template v-slot="scope">
          <el-button v-if="state.dataForm.status !== 'UNPUBLISH'"
            type="primary" link @click="detailHandle(scope.row.id, 'detail')">查看详情</el-button>
          <el-popconfirm width="220" icon-color="#626AEF" title="确定停止报名？" @confirm="signEndHandle(scope.row.id)"
            v-if="state.hasPermission('study:event:update') && state.dataForm.status === 'SIGN'">
            <template #reference>
              <el-button type="primary" link>停止报名</el-button>
            </template>
            <template #actions="{ confirm, cancel }">
              <el-button size="small" @click="cancel">取消</el-button>
              <el-button type="danger" size="small" @click="confirm">
                确认
              </el-button>
            </template>
          </el-popconfirm>

          <el-popconfirm width="220" icon-color="#626AEF" title="确定取消活动？" @confirm="cancelHandle(scope.row.id)"
            v-if="state.hasPermission('study:event:update') && state.dataForm.status === 'SIGN_END'">
            <template #reference>
              <el-button type="primary" link>取消活动</el-button>
            </template>
            <template #actions="{ confirm, cancel }">
              <el-button size="small" @click="cancel">取消</el-button>
              <el-button type="danger" size="small" @click="confirm">
                确认
              </el-button>
            </template>
          </el-popconfirm>

          <el-button v-if="state.hasPermission('study:event:update') && state.dataForm.status === 'UNPUBLISH'"
            type="primary" link @click="publishHandle(scope.row.id)">发布</el-button>
          <el-button v-if="state.hasPermission('study:event:update') && state.dataForm.status == 'UNPUBLISH'"
            type="primary" link @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button v-if="state.hasPermission('study:event:delete') && ['CANCEL', 'END'].includes(state.dataForm.status)"
            type="primary" link @click="state.deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script lang="ts" setup>
import { reactive, toRefs } from "vue";
import useView from "@/hooks/useView";
import { studyTypeList, studyStatusList } from "@/constants/config";
import { useRouter } from "vue-router";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";

const router = useRouter();
const view = reactive({
  getDataListURL: "/study/event/page",
  getDataListIsPage: true,
  deleteURL: "/study/event",
  deleteIsBatch: true,
  dataForm: {
    keywords: "",
    status: "SIGN",
  }
});

const state = reactive({ ...useView(view), ...toRefs(view) });

const handleTabChange = () => {
  state.dataList = [];
  state.getDataList()
};

const addOrUpdateHandle = (id?: string) => {
  router.push({
    path: '/study/event/edit',
    query: {
      id
    },
  });
};

const detailHandle = (id?: string, operation?: string) => {
  router.push({
    path: '/study/event/edit',
    query: {
      id,
      operation
    },
  });
};

const publishHandle = (id: string) => {
  baseService.post('/study/event/updateStatus', { status: 'SIGN', ids: [id] }).then((res) => {
    ElMessage.success({
      message: "发布成功",
      duration: 500,
      onClose: () => {
        state.dataForm.status = 'SIGN';
        state.getDataList();
      }
    });
  });
};

const signEndHandle = (id: string) => {
  baseService.post('/study/event/sign/end', [id]).then((res) => {
    ElMessage.success({
      message: "已停止报名",
      duration: 500,
      onClose: () => {
        state.getDataList();
      }
    });
  });
};

const cancelHandle = (id: string) => {
  baseService.post('/study/event/cancel', [id]).then((res) => {
    ElMessage.success({
      message: "已取消活动",
      duration: 500,
      onClose: () => {
        state.getDataList();
      }
    });
  });
};

const getStudyTypeName = (studyType: string) => {
  return studyTypeList.find((d) => d.value === studyType)?.label;
};
</script>
